package com.buer.service.impl;

import com.buer.dao.dmo.ExcelOriginDataEntity;
import com.buer.dao.dmo.ExportExcelDto;
import com.buer.dao.dmo.originDataProcess.GroupA;
import com.buer.dao.dmo.originDataProcess.GroupALarge;
import com.buer.dao.dmo.originDataProcess.GroupAMiddle;
import com.buer.dao.dmo.originDataProcess.GroupASmall;
import com.buer.dao.dmo.originDataProcess.GroupB;
import com.buer.service.dto.EachSheetDataDto;
import com.buer.service.intf.PreLoadingRateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by buer on 2019/10/15.
 */
@Service
public class PreLoadingRateServiceImpl implements PreLoadingRateService {

    private static final Logger LOG = LoggerFactory.getLogger(PreLoadingRateServiceImpl.class);

    /**
     * 区分为分组A或者分组B：
     * Ay为0，为分组A；大于600的丢弃；其他为分组B:
     *
     * @param eachSheetDataDto
     */
    @Override
    public void tellGroupAOrB(EachSheetDataDto eachSheetDataDto) {

        for (ExcelOriginDataEntity ele : eachSheetDataDto.getSheetOriginDataList()) {
            Float ay = ele.getAy();
            if (ay == 0) {
                eachSheetDataDto.getGroupAList().add(new GroupA(ele.getForcePeakValue(), ele.getDeltaLValues()));
            } else if (ay > 600) {
                continue;
            } else {
                eachSheetDataDto.getGroupBList().add(new GroupB(ele.getForcePeakValue(), ele.getDeltaLValues(), ele.getAx(), ele.getAy(), ele.getBx()));
            }
        }
    }

    /**
     * 分组A分类:DeltaLValues  [15.55,22.55]分三段：
     * （1）最大的一组
     * IL_F{ velocity }
     * IL_l{ velocity }
     * <p>
     * （2）中间的一组
     * GB1_F{ velocity }
     * GB1_L{ velocity }
     * <p>
     * （3）最小的一组
     * Little_F{ velocity }
     * Little_L{ velocity }
     *
     * @param eachSheetDataDto
     */
    @Override
    public void tellGroupADetial(EachSheetDataDto eachSheetDataDto) {
        for (GroupA ele : eachSheetDataDto.getGroupAList()) {
            Float deltaLValues = ele.getDeltaLValues();
            if (deltaLValues > 22.55) {
                eachSheetDataDto.getGroupALargeList().add(new GroupALarge(ele.getForcePeakValue(), deltaLValues));
            } else if (deltaLValues >= 15.55) {
                eachSheetDataDto.getGroupAMiddleList().add(new GroupAMiddle(ele.getForcePeakValue(), deltaLValues));
            } else {
                eachSheetDataDto.getGroupASmallList().add(new GroupASmall(ele.getForcePeakValue(), deltaLValues));
            }
        }
    }

    /**
     * 分组B处理一：求k	lr
     * k=(ForcePeakValue- Ay)/( Bx -Ax)
     * lr=k* Velocity
     *
     * @param eachSheetDataDto
     */
    @Override
    public void dealGroupBProcess1(EachSheetDataDto eachSheetDataDto) {
        for (GroupB ele : eachSheetDataDto.getGroupBList()) {
            Float k = (ele.getForcePeakValue() - ele.getAy()) / (ele.getBx() - ele.getAx());
            ele.setK(k);
            ele.setLr(k * Float.valueOf(eachSheetDataDto.getSheetName()));
        }
    }

    /**
     * 对于每一个sheet构造出它要返回的List
     *
     * @param eachSheetDataDto
     * @return
     */
    @Override
    public List<ExportExcelDto> buildExportExcelDto(EachSheetDataDto eachSheetDataDto) {

        // 获取到需要输出的数据
        List<ExcelOriginDataEntity> sheetOriginDataList = eachSheetDataDto.getSheetOriginDataList();
        int originDataSize = sheetOriginDataList.size();
        List<GroupB> groupBList = eachSheetDataDto.getGroupBList();
        int groupBSize = groupBList.size();
        List<GroupALarge> groupALargeList = eachSheetDataDto.getGroupALargeList();
        int aLargeSize = groupALargeList.size();
        List<GroupAMiddle> groupAMiddleList = eachSheetDataDto.getGroupAMiddleList();
        int aMiddleSize = groupAMiddleList.size();
        List<GroupASmall> groupASmallList = eachSheetDataDto.getGroupASmallList();
        int aSmallSize = groupASmallList.size();

        // 求出需要输出的数据最大的size
        int maxSize = Math.max(Math.max(Math.max(Math.max(originDataSize, groupBSize), aLargeSize), aMiddleSize), aSmallSize);

        List<ExportExcelDto> result = new ArrayList<ExportExcelDto>(maxSize);

        for (int index = 0; index < maxSize; index++) {
            ExportExcelDto exportExcelDto = new ExportExcelDto();
            if (index < originDataSize){
                ExcelOriginDataEntity excelOriginDataEntity = sheetOriginDataList.get(index);

                exportExcelDto.setName(excelOriginDataEntity.getName());
                exportExcelDto.setPv(excelOriginDataEntity.getPv());
                exportExcelDto.setLv(excelOriginDataEntity.getLv());
                exportExcelDto.setForcePeakValue(excelOriginDataEntity.getForcePeakValue());
                exportExcelDto.setDeltaLValues(excelOriginDataEntity.getDeltaLValues());
                exportExcelDto.setAx(excelOriginDataEntity.getAx());
                exportExcelDto.setAy(excelOriginDataEntity.getAy());
                exportExcelDto.setBx(excelOriginDataEntity.getBx());
            }
            if (index<groupBSize){
                GroupB groupB = groupBList.get(index);

                exportExcelDto.setGroupBForcePeakValue(groupB.getForcePeakValue());
                exportExcelDto.setGroupBDeltaLValues(groupB.getDeltaLValues());
                exportExcelDto.setGroupBAx(groupB.getAx());
                exportExcelDto.setGroupBAy(groupB.getAy());
                exportExcelDto.setGroupBBx(groupB.getBx());
                exportExcelDto.setGroupBK(groupB.getK());
                exportExcelDto.setGroupBLr(groupB.getLr());
                exportExcelDto.setGroupBF(groupB.getF());
            }
            if (index<aLargeSize){
                GroupALarge groupALarge = groupALargeList.get(index);

                exportExcelDto.setLargeAForcePeakValue(groupALarge.getForcePeakValue());
                exportExcelDto.setLargeADeltaLValues(groupALarge.getDeltaLValues());
                exportExcelDto.setLargeANILF(0f);
                exportExcelDto.setLargeANILl(0f);
            }
            if (index<aMiddleSize){
                GroupAMiddle groupAMiddle = groupAMiddleList.get(index);

                exportExcelDto.setMiddleAForcePeakValue(groupAMiddle.getForcePeakValue());
                exportExcelDto.setMiddleADeltaLValues(groupAMiddle.getDeltaLValues());
                exportExcelDto.setMiddleANGB1F(0f);
                exportExcelDto.setMiddleANGB1l(0f);
            }
            if (index<aSmallSize){
                GroupASmall groupASmall = groupASmallList.get(index);

                exportExcelDto.setSmallAForcePeakValue(groupASmall.getForcePeakValue());
                exportExcelDto.setSmallADeltaLValues(groupASmall.getDeltaLValues());
            }

            result.add(exportExcelDto);
        }

        return result;
    }

}
